/*
 *  NotificationsManager.cpp
 *  Rempart
 *
 *  Created by julien BONACHERA on 01/06/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include "NotificationsManager.h"
NotificationsManager* NotificationsManager::instance(0);

NotificationsManager* NotificationsManager::getInstance(){
	if (instance == 0) {
		instance = new NotificationsManager;
	}
	return instance;
}

void NotificationsManager::triggerNotification(){
	this->localLock.Lock();
	
	this->SetImage(*this->surface);
	this->SetCenter(this->surface->GetWidth()/2, this->surface->GetHeight()/2);
	this->visible = true;
	sf::Clock* localClock= new sf::Clock;
	localClock->Reset();
	
	if (this->shading) {
		for (int i = 0; i < 255; i++) {
			this->SetColor(sf::Color(255,255,255,i));
			sf::Sleep(0.001);
		}
	}
	sf::Sleep(this->seconds-(localClock->GetElapsedTime()*2));
	
	if (this->shading) {
		for (int i = 255; i > 0; i--) {
			this->SetColor(sf::Color(255,255,255,i));
			sf::Sleep(0.001);
		}
	}
	
	this->visible = false;
	delete localClock;
	
	this->localLock.Unlock();
}

void NotificationsManager::Run(){
	this->triggerNotification();
}
NotificationsManager::NotificationsManager(){
	this->SetPosition(GUI::width/2,GUI::heigth/2);
	this->visible = false;

}

NotificationsManager::~NotificationsManager(){}

void NotificationsManager::display(sf::Image* surface,int seconds, bool shading, bool isThread){
	this->shading = shading;
	this->surface=surface;
	this->seconds =seconds;
	if (isThread) {
		this->Launch();
	}
	else {
		this->triggerNotification();
	}

		
	

}
bool NotificationsManager::isVisible(){
	return this->visible;
}